package com.lhh.company.website.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhh.company.website.entity.SysException;
import com.lhh.company.website.entity.SysUser;
import com.lhh.company.website.mapper.SysUserMapper;
import com.lhh.company.website.utils.SecurityUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
* @author lx
* @description 针对表【sys_user(系统用户表 系统用户表)】的数据库操作Service实现
* @createDate 2025-07-23 15:50:31
*/
@Service
public class SysUserService extends ServiceImpl<SysUserMapper, SysUser> {

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 新增用户
     * @param sysUser
     */
    public void addSysUser(SysUser sysUser){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",sysUser.getName());
        SysUser sysUser1 = this.getOne(queryWrapper);
        if(sysUser1 != null){
            new SysException("用户已存在");
        }
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder(10);
        sysUser.setPasswd(SecurityUtils.encode(sysUser.getPasswd()));
        this.save(sysUser);
    }

    /**
     * 修改用户
     * @param sysUser
     * @return
     */
    public SysUser updateSysUser(SysUser sysUser){
        if(StringUtils.isBlank(sysUser.getId())){
            new SysException("用户不存在");
        }
        this.updateById(sysUser);
        return sysUser;
    }

    /**
     * 删除用户
     * @param sysUser
     */
    public void deleteSysUser(SysUser sysUser){
        if(StringUtils.isBlank(sysUser.getId())){
            new SysException("用户不存在");
        }
        this.removeById(sysUser);
    }
}




